package com.zxc.controller.customer;

import com.zxc.dto.LostCustomerAndCstactity;
import com.zxc.entity.CstLost;
import com.zxc.service.CstactityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/lost")
public class LostCustomerController {

    @Autowired
    private CstactityService cstactityService;

    @RequestMapping({"/{skipCount}/{pageSize}/{searchName}","/{skipCount}/{pageSize}"})
    @ResponseBody
    public Map<String,Object> getCustomerListOrByCustName(@PathVariable Integer skipCount,
                                                          @PathVariable Integer pageSize,
                                                          @PathVariable(required = false) String searchName){
        Map<String,Object> modelMap = new HashMap<>();
        int skipCounts = (skipCount-1)*pageSize;
        List<Map<String,Object>> cstactityList = cstactityService.getCstactityList(skipCounts,pageSize,searchName);
        for (Map<String,Object> cstactity : cstactityList){

            if (cstactity.get("lst_status").equals("1")){
                cstactity.replace("lst_status","警告");
            }else if (cstactity.get("lst_status").equals("2")){
                cstactity.replace("lst_status","暂缓流失");
            }else {
                cstactity.replace("lst_status","确认流失");
            }


        }
        modelMap.put("dataList",cstactityList);
        int total = cstactityService.selectCountCstactityForCustomer();
        modelMap.put("total",total);
        return modelMap;
    }

    @RequestMapping(value = "/initInfomation/{atvId}")
    @ResponseBody
    public Map<String,Object> initInfomation(@PathVariable Integer atvId){
        Map<String,Object> modelMap = new HashMap<>();
        List<Map<String,Object>> cstactityList = cstactityService.selectCstactityByAtvId(atvId);

        //这是暂缓措施集合
        List<CstLost> cstLostList = new ArrayList<>();

        for (Map<String,Object> cstLost : cstactityList){
            CstLost cstLost1 = new CstLost();
            if (cstLost.get("lst_status").equals("2")){
                cstLost1.setLstDelay((String) cstLost.get("lst_delay"));
            }
            cstLostList.add(cstLost1);
        }
        modelMap.put("dataList",cstLostList);

        //这是客户流失预警信息的DTO
        LostCustomerAndCstactity lostCustomerAndCstactity = new LostCustomerAndCstactity();
        lostCustomerAndCstactity.setCustName((String) cstactityList.get(0).get("atv_cust_name"));
        lostCustomerAndCstactity.setAtvDate((Date) cstactityList.get(0).get("atv_date"));
        lostCustomerAndCstactity.setCustManagerName((String) cstactityList.get(0).get("cust_manager_name"));
        modelMap.put("customer",lostCustomerAndCstactity);

        return modelMap;
    }


}
